<?php
class Room_m extends MY_Model{
	
	function __construct(){
		parent::__construct();
		$this->_table = 'emp_room';
	}
	
	//de quy
	function get_all($parent_id = 0,$frefix = ''){
		$this->db->where('parent_id',$parent_id);
		$model = parent::get_all();
		foreach($model as $item){
			$item->frefix = $frefix;
			$item->child = $this->get_all($item->id,$item->frefix.$this->frefix_child);
			
		}
		return $model;
	}
	//ul li
	//trang dang ky và emp_room/map
	function get_all_list($parent_id = 0 ,$data = NULL,$tag = NULL,$link = NULL){
		$link = !isset($link) ? 'users/register_step2/' : $link;
		$html = '';
		$tag_open = isset($tag) ? "<$tag>" : NULL;
		$tag_close = isset($tag) ? "</$tag>" : NULL;
		
		$data = !isset($data) ? $this->get_all($parent_id) : $data;
		$html .= $tag_open;
		foreach($data as $item){
			$html .= '<li>';
			$html .= "<a href=\"".base_url($link.$item->id)."\">".$item->title."</a>";
			if(count($item->child)>0)
				$html .= $this->get_all_list($item->id,$item->child,"ul",$link);
			$html .= '</li>';
		}
		$html .= $tag_close;
		return $html;
	}
	function array_room_child($parent_id = 0,&$result){
		$this->db->where('parent_id',$parent_id);
		$childs = parent::get_all();
		//print_r($childs);exit;
		$room_id_childs = array();
		foreach($childs as $i){ 
			$result[] = $i->id;
			$this->array_room_child($i->id,$result);
		}
		return $result;
		
	}
	public function delete($id){
		$parent_id = parent::get($id)->parent_id;//parent obj delete
		$this->db->where('parent_id',$id);
		$childs = (array)parent::get_all($id); // list child
		if(count($childs)>0){
			foreach($childs as $item){
				$item->parent_id = $parent_id;
				parent::update($item->id,$item);
			}
		}
		parent::delete($id);
	}
}